package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 

import com.lb.jwitter.service.TblQLDTTXADMTietHoc;
import com.lb.jwitter.service.TblQLDTTXADMTietHocService;
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuDuKien;
import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuDuKienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAThoiKhoaBieuDuKienDeleteAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private int tblQLDTTXAThoiKhoaBieuDuKienid; 
    public int getTblQLDTTXAThoiKhoaBieuDuKienid() { return tblQLDTTXAThoiKhoaBieuDuKienid; }

    public void setTblQLDTTXAThoiKhoaBieuDuKienid(int tblQLDTTXAThoiKhoaBieuDuKienid) { this.tblQLDTTXAThoiKhoaBieuDuKienid = tblQLDTTXAThoiKhoaBieuDuKienid; }

    private Long hocKyId;
    private Long phongId;
    private Long lopMonHocDuKienId;
    private Long tietBatDauId;
    private Long ngayId;
    
    private TblQLDTTXAThoiKhoaBieuDuKienService tblQLDTTXAThoiKhoaBieuDuKienService;
    
    private TblQLDTTXADMTietHocService tblQLDTTXADMTietHocService;

	private int preDelete(Message msg, String ids ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postDelete(Message msg, String ids ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    
public String execute() throws Exception 
   { 
     try{ 
	     Map session = ActionContext.getContext().getSession();
	     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	     if( taiKhoanDangNhap == null) return "noprivilege";
	     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
	     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_ThoiKhoaBieuDuKien", "xoa")){ 
	        return "noprivilege"; 
	        } 
	     tblQLDTTXADMTietHocService = (TblQLDTTXADMTietHocService) context.getBean("tbl_QLDTTXA_DM_TietHocClient");
	     tblQLDTTXAThoiKhoaBieuDuKienService = (TblQLDTTXAThoiKhoaBieuDuKienService) context.getBean("tbl_QLDTTXA_ThoiKhoaBieuDuKienClient"); 
	     msg = new Message(); 

	     Boolean cont = false;
	     List<ConditionReport> cons = new ArrayList<ConditionReport> ();
	     TblQLDTTXADMTietHoc tietHoc = tblQLDTTXADMTietHocService.gettblQLDTTXADMTietHoc(tietBatDauId);
		 do{
			 cons.clear();
		     if(hocKyId != null && !hocKyId.equals(new Long(0))){
		    	ConditionReport con = new ConditionReport();
		    	con.setCol("hocKyId.id");
		    	con.setValue1(hocKyId);
		    	con.setOperator("eq");
		    	cons.add(con);
		     }else {
		    	  return Action.ERROR;
		     }
	
		     if(phongId != null && !phongId.equals(new Long(0))){
		    	ConditionReport con = new ConditionReport();
		    	con.setCol("phongId.id");
		    	con.setValue1(phongId);
		    	con.setOperator("eq");
		    	cons.add(con);
		     }else {
		    	  return Action.ERROR;
		     }
		     
		     if(lopMonHocDuKienId != null && !lopMonHocDuKienId.equals(new Long(0))){
		    	ConditionReport con = new ConditionReport();
		    	con.setCol("lopMonHocDuKienId.id");
		    	con.setValue1(lopMonHocDuKienId);
		    	con.setOperator("eq");
		    	cons.add(con);
		     }else {
		    	  return Action.ERROR;
		     }
	
		     if(ngayId != null && !ngayId.equals(new Long(0))){
		    	ConditionReport con = new ConditionReport();
		    	con.setCol("ngayId.id");
		    	con.setValue1(ngayId);
		    	con.setOperator("eq");
		    	cons.add(con);
		     }else {
		    	  return Action.ERROR;
		     }
	
		     if(tietHoc != null && !tietHoc.getId().equals(new Long(0))){
		    	ConditionReport con = new ConditionReport();
		    	con.setCol("tietHocId.id");
		    	con.setValue1(tietHoc.getId());
		    	con.setOperator("eq");
		    	cons.add(con);
		     }else {
		    	  return Action.ERROR;
		     }
		 		     
		     List<TblQLDTTXAThoiKhoaBieuDuKien> tblTKBDuKiens = tblQLDTTXAThoiKhoaBieuDuKienService.findtblQLDTTXAThoiKhoaBieuDuKienByCondition(cons, 0, 0);
		     
		     if(tblTKBDuKiens == null || tblTKBDuKiens.size() == 0)
		    	 cont = false;
		     else {
		    	 cont = true;
		    	 for(TblQLDTTXAThoiKhoaBieuDuKien tblTKBDuKien : tblTKBDuKiens){
			    	 tblQLDTTXAThoiKhoaBieuDuKienService.deletetblQLDTTXAThoiKhoaBieuDuKien(tblTKBDuKien.getId());
			    	 tietHoc = tblTKBDuKien.getTietHocId().getTietKeTiep();
			     }
			    	 
		     }
		     if(tietHoc == null){
		    	 break;
		     }
		 } while(cont);
		     
	     
	     return Action.SUCCESS; 
      } 
      catch(Exception e){ 
    	  System.out.println("Loi Delete tbl_QLDTTXA_ThoiKhoaBieuDuKien Action"); 
    	  e.printStackTrace(); 
    	  return Action.ERROR; 
      } 
 }

	public Long getHocKyId() {
		return hocKyId;
	}

	public void setHocKyId(Long hocKyId) {
		this.hocKyId = hocKyId;
	}

	public Long getPhongId() {
		return phongId;
	}

	public void setPhongId(Long phongId) {
		this.phongId = phongId;
	}

	public Long getLopMonHocDuKienId() {
		return lopMonHocDuKienId;
	}

	public void setLopMonHocDuKienId(Long lopMonHocDuKienId) {
		this.lopMonHocDuKienId = lopMonHocDuKienId;
	}

	public Long getTietBatDauId() {
		return tietBatDauId;
	}

	public void setTietBatDauId(Long tietBatDauId) {
		this.tietBatDauId = tietBatDauId;
	}

	public TblQLDTTXAThoiKhoaBieuDuKienService getTblQLDTTXAThoiKhoaBieuDuKienService() {
		return tblQLDTTXAThoiKhoaBieuDuKienService;
	}

	public void setTblQLDTTXAThoiKhoaBieuDuKienService(
			TblQLDTTXAThoiKhoaBieuDuKienService tblQLDTTXAThoiKhoaBieuDuKienService) {
		this.tblQLDTTXAThoiKhoaBieuDuKienService = tblQLDTTXAThoiKhoaBieuDuKienService;
	}

	public TblQuyenChiTietService getTblQuyenChiTietService() {
		return tblQuyenChiTietService;
	}

	public void setTblQuyenChiTietService(
			TblQuyenChiTietService tblQuyenChiTietService) {
		this.tblQuyenChiTietService = tblQuyenChiTietService;
	}

	public ApplicationContextProvider getContextProvider() {
		return contextProvider;
	}

	public void setContextProvider(ApplicationContextProvider contextProvider) {
		this.contextProvider = contextProvider;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	public Long getNgayId() {
		return ngayId;
	}

	public void setNgayId(Long ngayId) {
		this.ngayId = ngayId;
	} 
 } 
 
